package com.bigdata.spark.core.rdd.operator.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{HashPartitioner, SparkConf, SparkContext}

/**
 * @author : ranzlupup
 * @date : 2023/3/1 16:14
 */
object RDD_Transform_Key_Value_partitionBy {
    def main(args: Array[String]): Unit = {
        val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("RDD")
        val sparkContext: SparkContext = new SparkContext(sparkConf)
        val rdd: RDD[Int] = sparkContext.makeRDD(List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))

        // TODO 将数据按照指定 Partitioner 重新进行分区。
        //      Spark 默认的分区器是 HashPartitioner
        rdd.map((_, 1))
            .partitionBy(
                new HashPartitioner(2)
            )
            .collect().foreach(println)
        sparkContext.stop()
    }
}
